#!/bin/bash
##############################################################
# File Name：install.sh
# Version :V1 . 
# Author: wangXiaoFeng 
# Email: 1289832464@qq.com
# Created Time ：2023-02-08 11:00:27
# Description：
#############################################################
#set parameter
ftp_user='wangxf'
ftp_user_passwd='wangxf@2023'
ftpDir=/data/wangxfa
ftp_maps_user='ye_lang'
ftp_maps_user_passwd='ye_lang@2023'
ftp_listen_port=2121
ftp_data_port=2020
pasv_min_port=30010 
pasv_max_port=30090


#get parameter
cpuType=`uname -m`

#create os user
echo -e "\n[mod 1]:create os user"
sh ./shell/create_OS_user.sh ${ftp_maps_user} ${ftp_maps_user_passwd}

#install vsftpd
echo -e "\n[mod 1]:Start installing vsftpd"
if [ ${cpuType} = 'x86_64' ];then
   timeout 30 yum install vsftpd -y
   rpm -Uvh ./rpm/*.rpm --nodeps 
   if [ ! -f /usr/sbin/vsftpd ];then
      echo -e "[Error]:Failed to install vsftpd through the rpm command. Please check the log information by executing the 'cat ./logs/rpm_install_vsftpd' command " 
   else
      vsftpdVersion=`vsftpd -v|awk '{print $3}'`
      if [ -z ${vsftpdVersion} ];then
         echo -e "[Error]:Failed to install vsftpd through the rpm command. Please check the log information by executing the 'cat ./logs/rpm_install_vsftpd' command " 
      else 
         echo -e "[info]:Vsfptd installed successfully,Vsftpd version:${vsftpdVersion}"
      fi
   fi
else
   echo -e "[Error]:cpuType is not x86_64"
fi



#set vsftpd.conf
echo -e "\n[mod 2]:edit vsftpd.conf"
if [ -f /etc/vsftpd/vsftpd_bak.conf ];then 
   echo "[info]:'/etc/vsftpd/vsftpd_bak.conf'file already exists Skip backup"
else
   mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd_bak.conf
fi
cat <<EOF >/etc/vsftpd/vsftpd.conf
anonymous_enable=NO
local_enable=YES
write_enable=YES
anon_upload_enable=NO
anon_mkdir_write_enable=NO
anon_other_write_enable=NO
connect_from_port_20=YES
xferlog_file=/var/log/vsftpd.log
xferlog_enable=YES
chroot_local_user=YES
listen=YES
listen_port=${ftp_listen_port}
ftp_data_port=${ftp_data_port}
pam_service_name=vsftpd
guest_enable=YES
guest_username=${ftp_maps_user}
user_config_dir=/etc/vsftpd/virtual_user_conf
virtual_use_local_privs=YES
ftpd_banner=Welcome to FTP service
allow_writeable_chroot=YES
max_clients=300
max_per_ip=10
use_localtime=YES
pasv_enable=YES
pasv_min_port=${pasv_min_port}
pasv_max_port=${pasv_max_port}
EOF

#Configure  Firewalld 
echo -e "\n[mod 3]:Configure  Firewalld"
firewalld_status=`systemctl status firewalld|grep Active|awk -F '(' '{print $2}'|awk -F ')' '{print $1}'`
echo ${firewalld_status}
if [ ${firewalld_status} = 'dead' ];then
   echo "firewalld is ${firewalld_status}"
else
   firewall-cmd --zone=public --add-port=${ftp_listen_port}/tcp --permanent
   firewall-cmd --zone=public --add-port=${ftp_data_port}/tcp --permanent
   firewall-cmd --zone=public --add-port=${pasv_min_port}-${pasv_max_port}/tcp --permanent
   firewall-cmd --add-service=ftp --permanent
   firewall-cmd --reload
fi   

#Configure  Selinux
echo -e "\n[mod 4]:Configure  Selinux"
selinuxStatus=`getenforce`
if [ ${selinuxStatus} = 'Disabled' ];then
   echo "Selinux is Disabled"
else
   sh ./shell/ftp_setsebool.sh
fi
#create virtual_user list file
echo -e "\n[mod 5]:create virtual_user list file"
touch /etc/vsftpd/virtual_user.txt
cat <<EOF >/etc/vsftpd/virtual_user.txt
${ftp_user}
${ftp_user_passwd}
EOF

#create ftp_defaultUser and ftp_user profile
echo -e "\n[mod 6]:create ftp_defaultUser and ftp_user profile"
mkdir -p ${ftpDir}/default ${ftpDir}/${ftp_user}
touch ${ftpDir}/default ${ftpDir}/${ftp_user}/${ftp_user}.md
chown -R ${ftp_maps_user}:${ftp_maps_user}  ${ftpDir}
mkdir -p /etc/vsftpd/virtual_user_conf/
touch /etc/vsftpd/virtual_user_conf/default
cat <<EOF >/etc/vsftpd/virtual_user_conf/default
local_root=${ftpDir}/default
write_enable=YES
anon_world_readable_only=YES
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
EOF

chown -R ${ftp_maps_user}:${ftp_maps_user}  ${ftpDir}
cp /etc/vsftpd/virtual_user_conf/default /etc/vsftpd/virtual_user_conf/${ftp_user}
sed -i "s/default/${ftp_user}/"  /etc/vsftpd/virtual_user_conf/${ftp_user}


#create ftp user apm.db
echo -e "\n[mod 7]:create ftp user apm.db"
db_load -T -t hash -f /etc/vsftpd/virtual_user.txt /etc/vsftpd/vsftpd_login.db

#set PAM
echo -e "\n[mod 8]:set PAM"
cat <<EOF >/etc/pam.d/vsftpd
auth       required pam_userdb.so   db=/etc/vsftpd/vsftpd_login
account    required pam_userdb.so   db=/etc/vsftpd/vsftpd_login
EOF

#open servers port
echo -e "\n[mod 9]:open servers port"
sed -i "/^ftp.*21\/tcp/cftp             ${ftp_listen_port}/tcp" /etc/services
sed -i "/^ftp.*21\/udp/cftp             ${ftp_listen_port}/udp" /etc/services
sed -i "/^ftp.*21\/sctp/cftp             ${ftp_listen_port}/sctp" /etc/services
sed -i "/^ftp-data.*20\/tcp/cftp-data        ${ftp_data_port}/tcp" /etc/services
sed -i "/^ftp-data.*20\/udp/cftp-data        ${ftp_data_port}/udp" /etc/services
sed -i "/^ftp-data.*20\/sctp/cftp-data        ${ftp_data_port}/sctp" /etc/services
 
#restart  vsftpd
echo -e "\n[mod 10]:restart  vsftpd"
systemctl enable vsftpd.service
systemctl restart vsftpd


sed -i "/^ftp_maps_user=*/cftp_maps_user=${ftp_maps_user}" ./shell/ftpuseradd
sed -i "/^ftpDir=*/cftpDir=${ftpDir}" ./shell/ftpuseradd

sed -i "/^ftp_maps_user=*/cftp_maps_user=${ftp_maps_user}" ./shell/ftpuserdel
sed -i "/^ftpDir=*/cftpDir=${ftpDir}" ./shell/ftpuserdel

cp -f ./shell/ftpuseradd /bin/
cp -f ./shell/ftpuserdel /bin/
chmod a+x /bin/ftpuseradd
chmod a+x /bin/ftpuserdel

echo -e "\nVsftp installation succeeded. Default content info:\033[42;37m ftp://127.0.0.1:2121 ${ftp_user}/${ftp_user_passwd}\033[0m\nIf you need to add an ftp account, please execute the [\033[42;37m ftpuseradd\033[0m] command\nIf you need to delete the ftp account, please execute the [\033[42;37m ftpuserdel\033[0m] command"
